﻿using SharpCopy.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace SharpCopy.Controller
{
    public class ClipboardDataController
    {
        #region Private members

        ClipboardData[] ListOfClipboardData;
        Dictionary<Key, int> KeyToIndex;

        #endregion

        #region ctor

        public ClipboardDataController()
        {
            ListOfClipboardData = new ClipboardData[10];

            KeyToIndex = new Dictionary<Key, int>();
            KeyToIndex.Add(Key.NumPad0, 0);
            KeyToIndex.Add(Key.NumPad1, 1);
            KeyToIndex.Add(Key.NumPad2, 2);
            KeyToIndex.Add(Key.NumPad3, 3);
            KeyToIndex.Add(Key.NumPad4, 4);
            KeyToIndex.Add(Key.NumPad5, 5);
            KeyToIndex.Add(Key.NumPad6, 6);
            KeyToIndex.Add(Key.NumPad7, 7);
            KeyToIndex.Add(Key.NumPad8, 8);
            KeyToIndex.Add(Key.NumPad9, 9);
            KeyToIndex.Add(Key.D0, 0);
            KeyToIndex.Add(Key.D1, 1);
            KeyToIndex.Add(Key.D2, 2);
            KeyToIndex.Add(Key.D3, 3);
            KeyToIndex.Add(Key.D4, 4);
            KeyToIndex.Add(Key.D5, 5);
            KeyToIndex.Add(Key.D6, 6);
            KeyToIndex.Add(Key.D7, 7);
            KeyToIndex.Add(Key.D8, 8);
            KeyToIndex.Add(Key.D9, 9);
        }

        #endregion

        #region Clipboard Methods

        public void RegisterClipBoardData(Key key, object data)
        {
            if (data != null)
            {
                ListOfClipboardData[KeyToIndex[key]] = new ClipboardData() { Data = data as string };
                NotificationController.ShowInfoBallonTip("Copy Operation", "Clipboard Text was stored in Key " + KeyToIndex[key]);
            }
        }

        public void RestoreToClipboard(Key key)
        {
            if (ListOfClipboardData[KeyToIndex[key]].Data != null)
            {
                Thread SetClipBoardData = new Thread(StartSetClipBoardData);
                SetClipBoardData.SetApartmentState(ApartmentState.STA);
                SetClipBoardData.Start(ListOfClipboardData[KeyToIndex[key]].Data);

                NotificationController.ShowInfoBallonTip("Paste Operation", "Text in Key " + KeyToIndex[key] + " was restored to the clipboard");
            }
        }

        private void StartSetClipBoardData(object obj)
        {
            System.Windows.Clipboard.SetText(obj as string);
        }

        #endregion
    }
}
